unit slSQLDataset;

interface

uses Classes, DB;

type
  TslSQLDataset = class(TDataSet)
   FSQL: TStringList; { The original SQL statement }

  private
   FLastError: Integer;

  protected { abstract methods required for all datasets }
   Function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
   Procedure InternalClose; override;
   Procedure InternalHandleException; override;
   Procedure InternalInitFieldDefs; override;
   Procedure InternalOpen; override;
   Function IsCursorOpen: Boolean; override;

   { ----- Constructor - Destructor ----- }
  public
   SetNumber: Integer;
   constructor Create(AOwner: TComponent); override;
   destructor  Destroy; override;
   procedure Open;
end;

implementation

uses libslsql;

{ TslSQLDataset }

constructor TslSQLDataset.Create(AOwner: TComponent);
begin
  inherited;
  SetNumber := 0;//Integer(Self);
  FSQL := TStringList.Create;
  FSQL.Text := 'SELECT * FROM parts LIMIT 1000 OFFSET 0';
end;

destructor TslSQLDataset.Destroy;
begin
  FSQL.Free;
  inherited;
end;

function TslSQLDataset.GetRecord(Buffer: PChar; GetMode: TGetMode;
  DoCheck: Boolean): TGetResult;
begin
case GetMode of
  gmCurrent: {FRecordset.MoveBy(0)};
  gmNext:    {FRecordset.Next};
  gmPrior:   {FRecordset.Prior};
  end;

Result := grOK; //FRecordset.MoveState;

Case Result of
    grOk   : begin
//             PInteger(Buffer)^ := FRecordSet.FCurrentRecordIndex;
//             if FLookupFieldsDisplayedRecordsOnly
//                then FillLookupFields(FRecordSet.FCurrentRecordIndex, FRecordSet.FCurrentRecordIndex);
//             if FCalcDisplayedRecordsOnly and Active
//                then GetCalcFields(Buffer);
             end;
    grError: If DoCheck then ;//FDatasetError.RaiseException(FDatasetName + ' : ' + Msg1040);
    end;
end;

procedure TslSQLDataset.InternalClose;
begin
  inherited;
//
end;

procedure TslSQLDataset.InternalHandleException;
begin
  inherited;
//
end;

procedure TslSQLDataset.InternalInitFieldDefs;
begin
  inherited;
//
end;

procedure TslSQLDataset.InternalOpen;
begin
  inherited;
//
end;

function TslSQLDataset.IsCursorOpen: Boolean;
begin
//  Result := FRecordset.Active and (aoRead in FSQLSelectStmt.AvailableOperations);
  Result := True; 
end;

procedure TslSQLDataset.Open;
var
  Field: TField;
  ColumnName: string;
  Size, ColumnCount, RecCount: Integer;
  i: Integer;
begin
  FLastError := sluSel(PChar(FSQL.Text), SetNumber, nil);

  ColumnCount := sluGetColumnCount(SetNumber);
  for i := 1 to ColumnCount do begin
    Field := TField.Create(Self);

    SetLength(ColumnName, 64);
    Size := Length(ColumnName);
    sluGetColumnName(i, PChar(ColumnName), Size, SetNumber);
    SetLength(ColumnName, Size);
    Field.Name := ColumnName;


    Self.Fields.Add(Field);
//      SetLength(FieldValue, 256);
//      Size := Length(FieldValue);
//      sluF(i, PChar(FieldValue), Size, 1);
//      SetLength(FieldValue, Size);
//      RowStr := RowStr + '"' + FieldValue + '";';
  end;

//  FieldList
end;

end.
